// dfs + 剪枝
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let m, n; //m行, n列
let initArr = [];
rl.on("line", (line)=>{
    if(m === undefined) {
        [m, n] = line.trim().split(",").map(Number)
    } else {
        initArr.push(line.trim().split(","))
        if(initArr.length === m) {
            console.log(getResult(m, n, initArr))
            //clear
            m = n = undefined
            initArr.length = 0
        }
    }

})

function getResult(m, n, initArr) {
    let count = 0
    let direction = [
        [1, 0],
        [0, 1],
        [1, 1],
        [1, -1]
    ]
    for (let i = 0; i < m ; i++) {
        for (let j = 0; j <n ; j++) {
            if(initArr[i][j] === "M") {
                count = Math.max(getMax(i, j,m, n, initArr,direction), count)
            }
        }
    }
    return count
}

function getMax(x, y, m, n, initArr, direction) {
    let count = 1
    for (const [a, b] of direction) {
        let oldX = x - a
        let oldY = y - b
        let newX = x + a
        let newY = y + b
        if(oldX >= 0 && oldX < m && oldY >= 0 && oldY < n && initArr[oldX][oldY] === "M" ) {
            continue
        }
        let len = 1
        while(newX >= 0 && newX < m && newY >= 0 && newY < n && initArr[newX][newY] === "M") {
            len++
            newX = newX + a
            newY = newY + b
        }
        count = Math.max(count, len)
    }

    return count
}